<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class HistoryHarga extends zulController {
	
	
	function __construct(){
		parent::__construct();
		$this->load->model("HistoryHargaModel");
		$this->checkLogin();
	}
	public function index()
	{
		$colModel['NO'] = array('No', 20, TRUE, 'center', 0);
        $colModel['NAMA_OBAT'] = array('Nama Obat', 110, FALSE, 'center', 1);
		$colModel['HISTORY_HARGA'] = array('Harga Obat', 110, FALSE, 'center', 1);
		$colModel['TGL_HISTORY'] = array('Tanggal Perubahan', 110, FALSE, 'center', 1);
		$colModel['EDIT'] = array('Edit', 70, FALSE, 'center', 0);
        $gridParams = array(
            'width' => 'auto',
            'height' => '390',
            'rp' => 15,
            'rpOptions' => '[10,15,20,25,40]',
            'pagestat' => 'Displaying: {from} to {to} of {total} items.',
            'blockOpacity' => 0.5,
            'title' => 'Daftar History Perubahan Harga Obat'
        );
        $buttons[] = array('Tambah', 'add', 'pengujian_aba');
        $buttons[] = array('Hapus', 'delete', 'pengujian_aba');
        $buttons[] = array('separator');
        $buttons[] = array('Pilih Semua', 'add', 'pengujian_aba');
        $buttons[] = array('Hapus Pilihan', 'delete', 'pengujian_aba');
        $buttons[] = array('separator');
		
        $grid_js = build_grid_js('flex1', site_url("HistoryHarga/gridHistoryHarga"), $colModel, 'ID_OBAT', 'ASC', $gridParams, $buttons);

        $data['title'] = "Daftar Obat";
        $data['info'] = "Daftar Obat";
        $data['js_grid'] = $grid_js;


        $data['added_js'] =
                "<script type='text/javascript'>
			function pengujian_aba(com,grid)
			{
				if (com=='Pilih Semua')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Hapus Pilihan')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}
				
				if (com=='Tambah')
				{
					location.href='" . base_url() . "index.php/HistoryHarga/formHistoryHarga/'; 
				}
				
				if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin mengaktifkan ' + $('.trSelected',grid).length + ' buah data?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '" .base_url('index.php/HistoryHarga/deleteHistoryHarga')."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					} 
				}
			} 
			</script>
			";
		$data["active"] = "master";
		$data["content"]	= $this->load->view("grid",$data,true);
		$this->load->view("main_new",$data);
	}
	function gridHistoryHarga(){
		$valid_fields = array('ID_HISTORY_HARGA', 'NAMA_OBAT', 'HISTORY_HARGA', 'TGL_HISTORY');
        $this->flexigrid->validate_post('ID_HISTORY_HARGA', 'ASC', $valid_fields);
        $records = $this->HistoryHargaModel->gridHistoryHarga();
        $this->output->set_header($this->config->item('json_header'));
        $no = 1;
        foreach ($records['records']->result() as $row) {
            $record_items[] = array($row->ID_HISTORY_HARGA,
                $no,
				$row->NAMA_OBAT,
				$row->HISTORY_HARGA,
				$this->dateFormat($row->TGL_HISTORY),
				"<a href='".site_url("/HistoryHarga/formHistoryHarga/".$row->ID_HISTORY_HARGA)."'><img src='". base_url()."assets/images/grid/edit.png'></a>"
            );
            $no++;
        }
        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
	function formHistoryHarga($parameter=FALSE){
		$this->load->library("form_validation");
		$this->load->model("ObatModel");
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		$this->form_validation->set_rules(array(
		array('field'=>'nama_obat','label'=>'Nama Obat','rules'=>'required'),
		array('field'=>'harga','label'=>'Harga Obat','rules'=>'required|numeric')
		));
		$this->form_validation->set_message('required','Kolom %s harus diisi !');
		$this->form_validation->set_message('numeric','Kolom %s harus berupa nomor !');
		if($this->form_validation->run() == FALSE){
			if($parameter == TRUE){
				$data["value"] = $this->HistoryHargaModel->join($parameter);
			}
			$data["parameter"]	= $parameter;
			$data["getObat"]	= $this->ObatModel->get(FALSE,FALSE);
			$data["active"] = "master";
			$data["content"]	= $this->load->view("formHistoryHarga",$data,true);
			$this->load->view("main_new",$data);
		} else {
			$id_obat	= $this->input->post("nama_obat");
			$harga	= $this->input->post("harga");
			$data	= array(
			"ID_OBAT"=>$id_obat,
			"HISTORY_HARGA"=>$harga,
			"TGL_HISTORY"=>date("Y-m-d")
			);
			$this->HistoryHargaModel->save($data, $parameter);
			redirect("/HistoryHarga");
		}
	}
	function deleteHistoryHarga(){
		$ids	= $this->input->post('items');
		$delete	= $this->HistoryHargaModel->delete($ids);
		if($delete == TRUE){
			$msg ='Data yang anda pilih berhasil dihapus';
			$this->output->set_output($msg);
		} else {
			$msg ='Gagal menghapus data';
			$this->output->set_output($msg);
		}
	}
}